package com.zzw.limit.alg.handler;

import com.zzw.limit.alg.Alg;
import com.zzw.limit.component.anotation.Limit;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * @author zzw
 * 默认限流处理实现类
 * 在不指定限流处理的情况下使用该类处理
 */
public class DefaultLimitHandler implements LimitHandler {

    @Autowired
    private ApplicationContext context;

    @Override
    public Boolean isLimit(Limit limit) {
        Class<? extends Alg> algClass = limit.alg();
        Alg alg = context.getBean(algClass);
        boolean flag = alg.tryAcquire(limit);
        return flag;
    }
}
